package com.lzm.ydpt.module.riding.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.riding.OrderBean;
import com.lzm.ydpt.module.riding.activity.RidingOrderDetailActivity;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 订单列表
 */
public class RidingOrderListFragment extends BaseFragment {
    public final static String INP_RIDING_ORDER_STATE = "RIDING_ORDER_STATE";
    @BindView(R.id.ltp_order_list)
    LoadingTip ltp_order_list;
    @BindView(R.id.srf_order_list)
    RefreshLayout srf_order_list;
    @BindView(R.id.rv_order_list)
    RecyclerView rv_order_list;
    private int orderState = 0;

    private static RidingOrderListFragment sFragment = null;
    private RvCommonAdapter<OrderBean> adapter = null;
    private List<OrderBean> mList = new ArrayList<>();
//   detailList.add("待取货");
//        detailList.add("配送中");
//        detailList.add("已完成");
//        detailList.add("已取消");
    public static RidingOrderListFragment newsInstance(int type) {
        sFragment = new RidingOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(INP_RIDING_ORDER_STATE,type);
        sFragment.setArguments(bundle);
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_order_list;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        orderState = getArguments().getInt(INP_RIDING_ORDER_STATE);
        initRv();
        initAdapter();
    }

    private void initRv() {
        setRefreshLayout(srf_order_list);
        srf_order_list.setEnableLoadMore(false);
        srf_order_list.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffWhite());
        srf_order_list.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                mPresenter.getHomeUserInfo();
            }
        });

    }

    private void initAdapter() {
        mList.add(new OrderBean());
        mList.add(new OrderBean());
        mList.add(new OrderBean());
        mList.add(new OrderBean());
        rv_order_list.setLayoutManager(new LinearLayoutManager(context));
        adapter = new RvCommonAdapter<OrderBean>(context, mList, R.layout.item_order_home_rv) {
            @Override
            protected void convert(RvViewHolder holder, OrderBean orderBean, int position) {
                 holder.setText(R.id.tv_count_price,"15");
                 holder.setText(R.id.tv_add_price,"（含5元加价）");
                 holder.setText(R.id.tv_send_order_addrss,"桃江路华润幸福里3期");
                 holder.setText(R.id.tv_send_order_distance,"2.2km");
                 holder.setText(R.id.tv_collect_order_addrss,"阳光瑞香新城B区");
                 holder.setText(R.id.tv_collect_order_distance,"3.1km");
                 holder.setText(R.id.tv_order_todo,"鲜花/50-100元/2公斤");
                TextView tv_submit_order_data = holder.getView(R.id.tv_submit_order_data);
                tv_submit_order_data.setText("去看看");
                switch (orderState){
                    case 0:
                        holder.setText(R.id.tv_order_deliver_time,"今天15:30取货");
                        tv_submit_order_data.setVisibility(View.VISIBLE);
                        tv_submit_order_data.setText("去取货");
                        break;
                    case 1:
                        holder.setText(R.id.tv_order_deliver_time,"15:30已取货");
                        tv_submit_order_data.setVisibility(View.VISIBLE);
                        tv_submit_order_data.setText("去完成");
                        break;
                    case 2:
                        holder.setText(R.id.tv_order_deliver_time,"15:30已完成");
                        tv_submit_order_data.setVisibility(View.VISIBLE);
                        tv_submit_order_data.setText("去查看");
                        break;
                    case 3:
                        holder.setText(R.id.tv_order_deliver_time,"15:30已被用户完成");
                        tv_submit_order_data.setVisibility(View.GONE);
                        break;
                }
                holder.setOnClickListener(R.id.tv_submit_order_data, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(Constants.INP_RIDING_ORDER_BEAN,orderBean);
                        openActivity(RidingOrderDetailActivity.class,bundle);
                    }
                });
            }
        };
        rv_order_list.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        sFragment = null;
        super.onDestroyView();
    }
}
